package controller

import (
	"go-frame/src/server-admin/service"
	"net/http"

	common "go-frame/src/server-common"

	"github.com/dgrijalva/jwt-go"
	"github.com/gin-gonic/gin"
)

type AuthController struct {
	AuthService *service.AuthService
}

func NewAuthController(authService *service.AuthService) *AuthController {
	return &AuthController{AuthService: authService}
}

type RegisterRequest struct {
	Username string `json:"username" binding:"required"`
	Password string `json:"password" binding:"required"`
	Email    string `json:"email" binding:"required,email"`
}

// Register godoc
// @Summary 用户注册
// @Description 用户注册接口
// @Tags Auth
// @Accept json
// @Produce json
// @Param user body RegisterRequest true "注册信息"
// @Success 200 {object} map[string]interface{} "注册成功"
// @Failure 400 {object} map[string]interface{} "请求参数错误"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /register [post]
func (ctrl *AuthController) Register(c *gin.Context) {
	var req RegisterRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		common.Error(http.StatusBadRequest, err.Error()).Send(c)
		return
	}

	if err := ctrl.AuthService.Register(req.Username, req.Password, req.Email); err != nil {
		common.Error(http.StatusInternalServerError, err.Error()).Send(c)
		return
	}

	common.Success(nil, "注册成功").Send(c)
}

type LoginRequest struct {
	Username string `json:"username" binding:"required"`
	Password string `json:"password" binding:"required"`
}

// Login godoc
// @Summary 用户登录
// @Description 用户登录接口，返回 JWT token
// @Tags Auth
// @Accept json
// @Produce json
// @Param user body LoginRequest true "登录信息"
// @Success 200 {object} map[string]interface{} "登录成功，返回 token"
// @Failure 400 {object} map[string]interface{} "请求参数错误"
// @Failure 401 {object} map[string]interface{} "认证失败"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /login [post]
func (ctrl *AuthController) Login(c *gin.Context) {
	var req LoginRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		common.Error(http.StatusBadRequest, err.Error()).Send(c)
		return
	}

	token, err := ctrl.AuthService.Login(req.Username, req.Password)
	if err != nil {
		common.Error(http.StatusUnauthorized, err.Error()).Send(c)
		return
	}

	common.Success(gin.H{"token": token}, "登录成功").Send(c)
}

// GetProfile godoc
// @Summary 获取当前登录用户信息
// @Description 获取当前登录用户的详细信息
// @Tags Auth
// @Security ApiKeyAuth
// @Produce json
// @Success 200 {object} models.User "成功获取用户信息"
// @Failure 401 {object} map[string]interface{} "未授权"
// @Failure 500 {object} map[string]interface{} "服务器内部错误"
// @Router /profile [get]
func (ctrl *AuthController) GetProfile(c *gin.Context) {
	claims, exists := c.Get("user_claims")
	if !exists {
		common.Error(http.StatusUnauthorized, "未授权：无法获取用户信息").Send(c)
		return
	}

	userClaims := claims.(*jwt.MapClaims)
	uuid, ok := (*userClaims)["uuid"].(string)
	if !ok {
		common.Error(http.StatusUnauthorized, "未授权：无效的 UUID").Send(c)
		return
	}

	user, err := ctrl.AuthService.GetUserByUUID(uuid)
	if err != nil {
		common.Error(http.StatusInternalServerError, err.Error()).Send(c)
		return
	}

	common.Success(user, "获取用户信息成功").Send(c)
}
